using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WWB.DisqusSharp.Model.DisqusDTO
{
    public class ForumPostCount
    {
        private IList<ThreadPostCount> counts;

        public ForumPostCount()
        {
            counts = new List<ThreadPostCount>();
        }

        public ThreadPostCount ForThread(string threadId)
        {
            return counts.FirstOrDefault(x => x.ThreadId == threadId);
        }

        public IEnumerable<ThreadPostCount> PostCounts
        {
            get { return counts; }
        }

        public static ForumPostCount FromJson(JToken jsonFragment)
        {
            var ret = new ForumPostCount();
            foreach (var ln in jsonFragment)
            {
                var jp = ln as JProperty;
                if (jp == null)
                {
                    throw new ArgumentOutOfRangeException("jsonFragment", "json framgment is wrong");
                }
                var pc = new ThreadPostCount();
                pc.ThreadId = jp.Name;
                pc.AllPosts = (int) ln.First.First;
                pc.VisiblePosts = (int) ln.First.Last;
                ret.counts.Add(pc);
            }
            return ret;
        }
    }

    public class ThreadPostCount
    {
        public string ThreadId { get; set; }
        public int AllPosts { get; set; }
        public int VisiblePosts { get; set; }
    }
}